package com.baidu.shopping.controller;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baidu.app.pojo.App;
import com.baidu.shopping.pojo.Cart;
import com.baidu.shopping.pojo.CartItem;
import com.baidu.shopping.pojo.Order;
import com.baidu.shopping.pojo.OrderItem;
import com.baidu.shopping.service.ShoppingServiceI;
import com.baidu.user.pojo.User;

@Controller
@RequestMapping("shopping")
public class ShoppingController {
	
	@Autowired
	private ShoppingServiceI service;

    
	/**
	 * 添加购物车
	 * @author 刘鹏
	 * @date 2016年7月14日  下午12:02:50
	 * @function
	 */
	@RequestMapping("addIntoCart")
	@ResponseBody
	public boolean addIntoCart(String appid, HttpServletRequest request) {
		
		App app = service.getAppById(appid);
		//创建购物车详单
		CartItem cartItem = new CartItem(System.currentTimeMillis() + "", app, 1);
		//判断用户是否登陆
	    User user = (User) request.getSession().getAttribute("user");
	    Cart cart = null;
	    
	    if(user == null) {
	    	//若用户没有登陆通过IP获取购物车
	    	cart = service.getCartById(request.getRemoteAddr());
	    	//若没有购物车则创建购物车
	        if (cart == null) {
				cart = new Cart();
				cart.setId(request.getRemoteAddr());
			}    	
	    } else {
	    	//若用户登陆则通过用户ID获取购物车
	        cart = service.getCartById(user.getId());
	        //若用户没有购物车则创建购物车
	        if(cart == null) {
	        	cart = new Cart();
	        	cart.setId(user.getId());
	        }
	    }
		
	    cart.add(cartItem);
    	return service.addAppIntoCart(cart);
	}
	
	
	/**
	 * 进入购物车页面
	 * @author 刘鹏
	 * @date 2016年7月14日  下午12:03:23
	 * @function
	 */
	@RequestMapping("toCartPage")
	public String toCartPage(HttpServletRequest request) {
		
		//获取用户购物车
		User user = (User) request.getSession().getAttribute("user");
		Cart cart = null;
		if(user == null){
			cart = service.getCartById(request.getRemoteAddr());
		} else {
		    cart = service.getCartById(user.getId());
		}
	
		//若用户拥有购物车的话取出购物车详单
		if(cart != null) {
			Collection<CartItem> cartItems = cart.getCart().values();
	        request.setAttribute("coll", cartItems);
	        int size = cartItems.size();
	        request.setAttribute("count", size);
	        double totalPrice = cart.getTotalPrice();
	        request.setAttribute("totalPrice", totalPrice);
		}		
        return "shopping/cart";
	}
	
	
	/**
	 * 结算时让用户先登陆
	 * @author 刘鹏
	 * @date 2016年7月15日  上午8:50:34
	 * @function
	 */
	@RequestMapping("login")
	@ResponseBody
	public boolean login(User user, HttpServletRequest request) {
		
		user.setEmail(user.getPhone());
	    User user2 = service.login(user);
	    
	    if(user2 != null) {
	    	request.getSession().setAttribute("user", user2);
			
			Cart cart = service.getCartById(user2.getId());
			if(cart == null) {
				cart = new Cart();
				cart.setId(user2.getId());
			}
			Cart cart2 = service.getCartById(request.getRemoteAddr());
			Collection<CartItem> items = cart2.getCart().values();
			for (CartItem cartItem : items) {
				cart.add(cartItem);
			}
			service.addAppIntoCart(cart);
			cart2.clear();
			service.addAppIntoCart(cart2);
			return true;
	    }
	    	
		return false;
	}
	
	
	/**
	 * 核对订单
	 * @author 刘鹏
	 * @date 2016年7月15日  上午10:18:47
	 * @function
	 */
	@RequestMapping("toCheckOrder")
	public String toCheckOrder(String address, String[] appid) {
		
		System.out.println(address);
		System.out.println(appid.toString());
		return "";
	}
	
	
	/**
	 * 订单入库
	 * @author 刘鹏
	 * @date 2016年7月15日  下午2:17:21
	 * @function
	 */
	@RequestMapping("addOrder")
	public String addOrder(String address, String[] appid, HttpSession session){
		
		User user = (User) session.getAttribute("user");
		String orderid = service.addOrder(user, address, appid);
		return "redirect:/shopping/orderitem.action?orderid="+orderid;
	}
	
	
	/**
	 * 订单详情
	 * @author 刘鹏
	 * @date 2016年7月15日  下午2:26:36
	 * @function
	 */
	@RequestMapping("orderitem")
	public String orderitem(String orderid, HttpServletRequest request) {
		
		List<OrderItem> list = service.getOrderitem(orderid);
		request.setAttribute("list", list);
		return "shopping/orderitem";
	}
	
	
	

	
}
